//
//  KYLoginViewController.m
//  kuaiyi
//
//  Created by 杨亮 on 16/3/12.
//  Copyright © 2016年 kuaiyi. All rights reserved.
//

#import "KYLoginViewController.h"
#import <Masonry.h>
#import "UIViewController+MMDrawerController.h"
#import "MMDrawerController.h"
#import "KYRegisterViewController.h"
#import <SVProgressHUD.h>
#import "KYLoginModel.h"
#import "MMDrawerController.h"
@interface KYLoginViewController ()
@property (nonatomic, weak)UITextField *phoneTextField;
@property (nonatomic, weak)UITextField *passwordField;
@end

@implementation KYLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithRed:242/255.0 green:242/255.0 blue:242/255.0 alpha:1];
    [self setUpUI];

}
//重写父类的返回方法
- (void)backToLastVC{
    [self.mm_drawerController toggleDrawerSide:MMDrawerSideLeft animated:YES completion:nil];
    [self.navigationController popToRootViewControllerAnimated:NO];
}
#pragma mark - 设置界面
-(void)setUpUI{
    UIImageView *topImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"product_img_logo_nor.png"]];
    UIImageView *kyImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"product_text_logo_nor.png"]];
    
    UITextField *phoneTextField = [[UITextField alloc]init];
    phoneTextField.placeholder = @"请输入您的手机号码";
    phoneTextField.backgroundColor = [UIColor whiteColor];
    phoneTextField.borderStyle = UITextBorderStyleRoundedRect;
    phoneTextField.keyboardType = UIKeyboardTypeNumberPad;
    phoneTextField.leftView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"illness_button_input_blue.png"]];
    phoneTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    phoneTextField.leftViewMode = UITextFieldViewModeAlways;
    phoneTextField.clearsOnBeginEditing = YES;
    self.phoneTextField = phoneTextField;
    
    UITextField *password = [[UITextField alloc]init];
    password.placeholder = @"请输入您的登录密码";
    password.backgroundColor = [UIColor whiteColor];
    password.borderStyle = UITextBorderStyleRoundedRect;
    password.keyboardType = UIKeyboardTypeDefault;
    password.leftView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"illness_button_input_blue.png"]];
    password.leftViewMode = UITextFieldViewModeAlways;
    password.clearButtonMode = UITextFieldViewModeWhileEditing;
    password.secureTextEntry = YES;
    password.clearsOnBeginEditing = YES;
    self.passwordField = password;
    
    UIButton *phoneBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [phoneBtn setTitle:@"通过短信验证码登录" forState:UIControlStateNormal];
    [phoneBtn setTitleColor:[UIColor colorWithRed:48/255.0 green:189/255.0 blue:189/255.0 alpha:1] forState:UIControlStateNormal];
    phoneBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    
    UIButton *loginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [loginBtn setTitle:@"登录" forState:UIControlStateNormal];
    [loginBtn setBackgroundImage:[UIImage imageNamed:@"link_button_01"] forState:UIControlStateNormal];
    [loginBtn addTarget:self action:@selector(loginBtnKlick) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *registerNew = [UIButton buttonWithType:UIButtonTypeCustom];
    [registerNew setTitle:@"注册新账号" forState:UIControlStateNormal];
    [registerNew setTitleColor:[UIColor colorWithRed:48/255.0 green:189/255.0 blue:189/255.0 alpha:1] forState:UIControlStateNormal];
    registerNew.titleLabel.font = [UIFont systemFontOfSize:14];
    [registerNew addTarget:self action:@selector(registerNewID) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *forgetPassword = [UIButton buttonWithType:UIButtonTypeCustom];
    [forgetPassword setTitle:@"忘记密码" forState:UIControlStateNormal];
    [forgetPassword setTitleColor:[UIColor colorWithRed:48/255.0 green:189/255.0 blue:189/255.0 alpha:1] forState:UIControlStateNormal];
    forgetPassword.titleLabel.font = [UIFont systemFontOfSize:14];
//    forgetPassword.backgroundColor = [UIColor redColor];
    
    UIView *line = [[UIView alloc]init];
//    line.backgroundColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:1];
    line.backgroundColor = [UIColor blackColor];
    

    
    [self.view addSubview:topImage];
    [self.view addSubview:kyImage];
    [self.view addSubview:phoneTextField];
    [self.view addSubview:password];
    [self.view addSubview:phoneBtn];
    [self.view addSubview:loginBtn];
    [self.view addSubview:registerNew];
    [self.view addSubview:forgetPassword];
    [self.view addSubview:line];
    
    [topImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top).offset(80);
        make.centerX.equalTo(self.view.mas_centerX);
        make.width.equalTo(@67);
        make.height.equalTo(@80);
    }];
    [kyImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(topImage.mas_bottom).offset(5);
        make.centerX.equalTo(self.view.mas_centerX);
        make.width.equalTo(@83);
        make.height.equalTo(@45);
    }];
    [phoneTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(kyImage.mas_bottom).offset(10);
        make.left.equalTo(self.view.mas_left).offset(20);
        make.right.equalTo(self.view.mas_right).offset(-20);
        make.height.equalTo(@35);
    }];
    [password mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(phoneTextField.mas_bottom).offset(10);
        make.left.equalTo(self.view.mas_left).offset(20);
        make.right.equalTo(self.view.mas_right).offset(-20);
        make.height.equalTo(@35);
    }];
    [phoneBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(password.mas_bottom).offset(10);
        make.centerX.equalTo(self.view.mas_centerX);
    }];
    [loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(phoneBtn.mas_bottom).offset(10);
        make.width.equalTo(password.mas_width);
        make.height.equalTo(password.mas_height);
        make.centerX.equalTo(self.view.mas_centerX);
    }];
    [registerNew mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.view.mas_bottom).offset(-10);
        make.right.equalTo(self.view.mas_centerX);
        make.width.equalTo(@150);
    }];
    [forgetPassword mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.view.mas_bottom).offset(-10);
        make.left.equalTo(self.view.mas_centerX);
        make.width.equalTo(@150);
    }];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(forgetPassword.mas_bottom).offset(-8);
        make.centerX.equalTo(self.view.mas_centerX);
        make.width.equalTo(@1);
        make.height.equalTo(@15);
    }];

}
#pragma mark - 按钮点击事件
-(void)registerNewID{
    [self.navigationController pushViewController:[[KYRegisterViewController alloc]init] animated:YES];
}
-(void)loginBtnKlick{
//    if (![self isMobileNo:self.phoneTextField.text]) {
//        [SVProgressHUD showInfoWithStatus:@"请输入正确的手机号码"];
//    }else if (self.passwordField.text.length == 0){
//        [SVProgressHUD showInfoWithStatus:@"请输入手机号码"];
//    }else{
        NSString *phoneNo = self.phoneTextField.text;
        NSString *password = self.passwordField.text;
        [KYLoginModel loginWithPhoneNo:phoneNo password:password success:^(BOOL success) {
            if (!success) {
                [SVProgressHUD showInfoWithStatus:@"您输入的账号和密码不符,请重新输入"];
            }else{
                NSLog(@"登录成功");
                [self.mm_drawerController toggleDrawerSide:MMDrawerSideLeft animated:YES completion:nil];
                [self.navigationController popToRootViewControllerAnimated:NO];
                [[NSNotificationCenter defaultCenter]postNotificationName:@"login" object:nil userInfo:nil];
            }
        }];
//    }
}
-(BOOL)isMobileNo:(NSString *)mobileNo{
    NSString *pattern = @"^1(3[0-9]|5[0-35-9]|8[025-9])\\d{8}$";
    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern];
    if ([regextestmobile evaluateWithObject:mobileNo]) {
        return YES;
    }else{
        return NO;
    }
}
@end
